/**
 * Created by alan on 2017/5/5.
 * 菜单管理
 */

Ext.define("NodeStore", {
    extend: 'Ext.data.TreeStore',
    proxy: {
        type: 'ajax',
        url: config.URL + 'szy/sys/menu/listTree?extId=0',
        reader: 'json',
        autoLoad: true
    },
    fields: [{
            name: 'id',
            type: 'string'
        }, {
            name: 'text',
            type: 'string'
        }, {
            name: 'viewType',
            type: 'string'
        }, {
            name: 'iconCls',
            type: 'string'
        }
        // , {
        //     name: 'sort',
        //     type: 'string'
        // }
    ]
});


Ext.define('Admin.view.sys.men', {
    extend: "Admin.common.container.BaseContainer",
    xtype: 'men',
    //起别名
    alias: 'men',
    requires: [
        'Admin.store.sys.Men',
        "Ext.panel.Panel",
        "Ext.grid.Panel",
        "Ext.button.Button",
        "Admin.common.form.field.ComboBoxTree",
    ],
    controller: 'menController',
    userCLs: 'big-100 small-100',
    layout: 'fit',
    width: '100%',
    height: 200,
    items: [{
        xtype: 'treepanel',
        title: "菜单管理",
        columnLines: true,
        border: true,
        reference: 'menGrid',
        tbar: [{
                xtype: "textfield",
                minWidth: 300,
                labelAlign: 'right',
                fieldLabel: "菜单名称",
                labelWidth: 80,
                reference: "name",
                margin: '10 0 10 0'
            },
            {
                xtype: "button",
                text: "查询",
                iconCls: 'x-fa fa-search',
                handler: 'check',
                margin: '0 10 0 10',
                // style: 'background-color:#108ee9; border-radius:4px;-webkit-border-radius: 4px;',
                style: 'border-radius:4px;-webkit-border-radius: 4px;'
            }, {
                xtype: "button",
                text: "新增",
                iconCls: 'x-fa fa-save',
                handler: 'add',
                margin: '0 10 0 10',
                // style: 'background-color:#108ee9; border-radius:4px;-webkit-border-radius: 4px;',
                style: 'border-radius:4px;-webkit-border-radius: 4px;'
            }
        ],
        root: {
            name: '根目录',
            expanded: true
        },
        rootVisible: false,
        columns: [{
                xtype: 'treecolumn',
                text: '菜单名称',
                dataIndex: 'text',
                align: 'left',
                flex: 1
            },
            {
                text: 'Id',
                dataIndex: 'id',
                hidden: true
            },
            {
                text: '链接',
                dataIndex: 'viewType',
                align: 'left',
                flex: 1
            },
            {
                text: '菜单图标',
                dataIndex: 'iconCls',
                align: 'left',
                flex: 1
            },
            // {
            //     text: '排序',
            //     dataIndex: 'sort',
            //     align: 'left',
            //     flex: 1
            // },
            {
                text: '操作',
                align: 'center',
                width: 100,
                renderer: function(value, metadata, record, rowIndex, columnIndex, store) {
                    if (rowIndex == 0) { return ""; }
                    window.editRecord = function(id) {
                        var record = store.getById(id).data;
                        // console.log(record);

                        var chose_tip_win = new Ext.Window({
                            title: "修改菜单",
                            width: 500,
                            //modal: true,//设置模态框背后的界面不可操作
                            closeAction: 'hide',
                            items: [{
                                layout: "form",
                                xtype: 'form',
                                padding: 8,
                                defaults: {
                                    labelWidth: 80,
                                    labelAlign: "right"
                                },
                                items: [{
                                        xtype: 'textfield',
                                        fieldLabel: '菜单id',
                                        name: 'id',
                                        hidden: true,
                                        value: record.id
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '菜单名称',
                                        name: 'name',
                                        allowblank: false,
                                        value: record.text
                                    },
                                    {
                                        fieldLabel: "父级菜单",
                                        xtype: 'comboboxtree',
                                        name: "parent.id",
                                        value: record.parentName,
                                        editable: false,
                                        border: true,
                                        url: config.URL + 'szy/sys/menu/listTree?extId=0',
                                        allowBlank: false
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '菜单链接',
                                        name: 'href',
                                        allowblank: false,
                                        value: record.viewType
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '菜单图标',
                                        name: 'icon',
                                        allowblank: false,
                                        value: record.iconCls
                                    }, {
                                        fieldLabel: "排序",
                                        name: "sort",
                                        xtype: 'numberfield',
                                        value: record.sort,
                                        minValue: 0,
                                    }, {
                                        xtype: "radiogroup",
                                        fieldLabel: "是否为叶子节点",
                                        vertical: true,
                                        items: [{
                                                boxLabel: "是",
                                                name: 'leaf',
                                                inputValue: "1",
                                                checked: record.leaf,
                                                style: 'margin:0px 15px;'
                                            },
                                            {
                                                boxLabel: "否",
                                                name: 'leaf',
                                                checked: !record.leaf,
                                                inputValue: "0"
                                            }
                                        ]
                                    }, {
                                        xtype: "radiogroup",
                                        fieldLabel: "是否可见",
                                        vertical: true,
                                        items: [{
                                                boxLabel: "是",
                                                name: 'isShow',
                                                inputValue: "1",
                                                checked: record.isShow,
                                                style: 'margin:0px 15px;'
                                            },
                                            {
                                                boxLabel: "否",
                                                name: 'isShow',
                                                checked: !record.isShow,
                                                inputValue: "0"
                                            }
                                        ]
                                    }

                                ]
                            }],
                            buttons: [{
                                xtype: "button",
                                text: "确定",
                                handler: function() {
                                    var form = chose_tip_win.items.first().getForm();
                                    if (form.isValid()) {
                                        var values = chose_tip_win.items.first().getForm().getValues();
                                        if (!values['parent.id']) {
                                            values['parent.id'] = record.parentId;
                                        }
                                        values.river = config.RIVER;
                                        Ext.Ajax.request({
                                            url: config.URL + 'szy/sys/menu/update',
                                            method: "post",
                                            success: function(response, opts) {
                                                Ext.MessageBox.hide();
                                                var result = Ext.JSON.decode(response.responseText);
                                                if (result.code === 1) {
                                                    Ext.Msg.alert('系统提示', "修改成功");
                                                    store.reload();
                                                    form.reset();
                                                    chose_tip_win.hide();
                                                } else {
                                                    Ext.Msg.alert("系统提示", '修改失败“' + result.msg + '”');
                                                }
                                            },
                                            failure: function() {
                                                Ext.Msg.alert('系统提示', '系统出错！');
                                            },
                                            params: values
                                        });
                                    } else {
                                        Ext.Msg.alert('提示', '表单填写不合法！');
                                    }

                                }
                            }, {
                                xtype: "button",
                                text: "取消",
                                handler: function() {
                                    chose_tip_win.items.first().getForm().reset();
                                    chose_tip_win.hide();
                                }
                            }]
                        });
                        chose_tip_win.show();
                    }
                    window.deleteRecord = function(id) {
                        var rid = store.getById(id).data.id;
                        var values = {};
                        values.id = rid;
                        values.river = config.RIVER;
                        // console.log(rid);
                        Ext.Msg.confirm('提示', '确定要删除？', function(button) {
                            if (button == 'yes') {
                                Ext.Ajax.request({
                                    url: config.URL + 'szy/sys/menu/delete',
                                    method: "post",
                                    success: function(response, opts) {
                                        Ext.MessageBox.hide();
                                        var result = Ext.JSON.decode(response.responseText);
                                        if (result.code === 1) {
                                            Ext.Msg.alert('系统提示', "删除成功");
                                            store.reload();
                                        } else {
                                            Ext.Msg.alert("系统提示", '删除失败“' + result.msg + '”');
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            }
                        });

                    }
                    return '<a style="color: #919191; cursor: pointer;" onclick="editRecord(\'' + record.id + '\')"><span style="font-size:18px;" class = "x-fa fa-edit"></span></a>' +
                        '<a style="color: #919191; cursor: pointer;" onclick="deleteRecord(\'' + record.id + '\')"><span style="font-size:18px;" class="x-fa fa-trash"></span></a>';

                }
            }

            // { text: '权限标识', dataIndex: 'permission',align: 'left', flex: 1 }
        ],
        loadMask: {
            msg: '正在加载数据,请稍等...'
        },
        store: Ext.create('NodeStore'),
    }]
})


Ext.define('Admin.view.sys.menuAdd', {
    extend: 'Admin.common.form.BaseFormPanel',
    xtype: 'menuAdd',
    alias: 'menuAdd',
    defaults: {
        labelWidth: 80,
        labelAlign: "right"
    },
    layout: 'form',
    defaultType: "textfield",
    items: [{
            fieldLabel: "菜单名称",
            name: "name",
            allowBlank: false,
        },
        {
            fieldLabel: "父级菜单",
            xtype: 'comboboxtree',
            name: "parent.id",
            editable: false,
            border: true,
            url: config.URL + 'szy/sys/menu/listTree?extId=0',
            allowBlank: false
        },
        {
            fieldLabel: "链接",
            name: "href",
            allowBlank: false,
        },
        {
            fieldLabel: "图标",
            name: "icon",
            // allowBlank: false,
        }, {
            fieldLabel: "排序",
            xtype: 'numberfield',
            minValue: 0,
            name: "sort",
        }, {
            xtype: "radiogroup",
            fieldLabel: "是否为叶子节点",
            vertical: true,
            items: [{
                    boxLabel: "是",
                    name: 'leaf',
                    inputValue: "1",
                    checked: true,
                    style: 'margin:0px 15px;'
                },
                {
                    boxLabel: "否",
                    name: 'leaf',
                    inputValue: "0"
                }
            ]
        }, {
            xtype: "radiogroup",
            fieldLabel: "是否可见",
            vertical: true,
            items: [{
                    boxLabel: "是",
                    name: 'isShow',
                    inputValue: "1",
                    checked: true,
                    style: 'margin:0px 15px;'
                },
                {
                    boxLabel: "否",
                    name: 'isShow',
                    inputValue: "0"
                }
            ]
        }

    ]
});